<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;
use think\Cookie;
use app\index\model\Login as LoginModel;

class Reginster extends Controller
{
    public $LoginModel = null;

    public function _initialize()
    {
        // 从cookie中拿用户ID或者用户名
        if ( Cookie::has('user:msg') ) {
            $cookiemsg = unserialize(Cookie::get('user:msg'));
        } else {
            $cookiemsg = null;
        }
        // 判断是否有session
        if ( Session::has($cookiemsg['name'].':usermsg') ) {
                $this->assign('userdata',Session::get($cookiemsg['name'].':usermsg'));
                $this->fetch('home/main');
                $this->redirect('index/Index/index');
            }
            
            $LoginModel = new LoginModel;
            $this->LoginModel = $LoginModel;
        }
        
    /**
     * 注册
     * @param array $request 注册的信息
     * @return 返回添加成功或者失败信息
     */
    public function save(Request $request)
    {
        $msg = $request->param();
        $data = $this->LoginModel->addUser($msg);
        // return $data;
        // var_dump($data);die;
        if($data){
            return $this->success('注册成功', 'index/Login/index');
        }else{
            return $this->error('注册失败');
            }
    }
    

        // 加载页面
        public function index()
        {
            // var_dump($_COOKIE);die;
            // var_dump('123456');die;
            return view('reginster/reginster',[
                'title' => '用户注册',
            ]);
        }

    
}